package cn.edu.zzuli.nothinglink.controller;


import cn.edu.zzuli.nothinglink.anno.CurrentUser;
import cn.edu.zzuli.nothinglink.entity.R;
import cn.edu.zzuli.nothinglink.service.CourseService;
import cn.edu.zzuli.nothinglink.vo.CourseInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author suzyyy
 * @since 2021-04-08
 */
@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    CourseService courseService;


    @PostMapping("/info")
    public R createCourse(@RequestBody @Valid CourseInfoVo vo, @CurrentUser Integer uId) {
        try {
            return courseService.createCourse(vo, uId);
        } catch (Exception e) {
            System.out.println(e);
            return R.error();
        }

    }

    @PutMapping("/info")
    public R updateCourse(@RequestBody @Valid CourseInfoVo vo) {
        return courseService.updateTheCourseInfo(vo);
    }

    @GetMapping("/info/{courseId}")
    public R getCourseInfo(@PathVariable Integer courseId) {
        return R.success().add("course", courseService.getCourseInfo(courseId));
    }

    @DeleteMapping("/info/{courseId}")
    public R deleteCourse(@PathVariable Integer courseId) {
        if (courseService.removeById(courseId)) return R.success();
        return R.error();
    }

    /**
     * 获取老师的全部课程
     * @param uId
     * @return
     */
    @GetMapping("/my")
    public R getMyCourses(@CurrentUser Integer uId) {
        if (uId == null) return R.error().add("error", "no login");

        return courseService.getMyCoursesList(uId);
    }

    /**
     * 获取学生所在班级绑定的全部课程
     * @param stuId
     * @return
     */
    @GetMapping("/in/myclass")
    public R getCoursesInMyClass(@CurrentUser Integer stuId) {
        if (stuId == null) return R.error().add("error", "no login");
        return courseService.getMyCoursesInMyClass(stuId);
    }

    @GetMapping("/info/stus/{courseId}")
    public R getStusInCourse(@PathVariable Integer courseId) {
        return courseService.getStusInCourse(courseId);
    }

    @GetMapping("/condition/work/{courseId}")
    public R getWorkCondition(@PathVariable Integer courseId) {
        return courseService.getWorkConditionByCourseId(courseId);
    }

}

